#!/usr/bin/python
# -*- coding: UTF-8 -*-
import re
import requests
import bs4
import json
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker
from config import DB_URI
#import MainUtil

# 相对路径，也是需要将此路径存入数据库
resources_file_path = '/resources/movie/cinemaNameList.ini'
scratch_url = 'https://www.dygod.net/html/gndy/jddy/'


engine = create_engine(DB_URI)
Base = declarative_base(engine)  # SQLORM基类
session = sessionmaker(engine)()  # 构建session对象


class Movie(Base):
    __tablename__ = 'movie'  # 表名
    id = Column(Integer, primary_key=True, autoincrement=True)
    moviename = Column(String(500))
    url= Column(String(500))

# class MovieComing(Base):
#     __tablename__ = 'moviecoming'  # 表名
#     id = Column(Integer, primary_key=True, autoincrement=True)
#     moviename = Column(String(500))

# scratch data with define url
def scratch_latest_movies(scratch_url):
    r = requests.get(scratch_url)
    r.encoding = r.apparent_encoding

    soup = bs4.BeautifulSoup(r.text, "html.parser")
    new_movies_json = soup.find('div', class_='co_content8').find('ul').findAll("a", {"class" : "ulink"})#.find_all('tr', recursive=False)
    # coming_movies_data = soup.find_all('li', class_='i_wantmovie')
    # 上映的电影
    for movie in new_movies_json:
        hreftext=re.findall('href=".*?"',str(movie))
        movietext=re.findall('">.*?</a>',str(movie))
        movietext=str(movietext)[4:]
        movietext=movietext[:-6]
        hreftext=str(hreftext)[8:]
        hreftext=hreftext[:-3]
        hreftext="https://www.dygod.net/"+hreftext
        fli = Movie(moviename=movietext,url=hreftext)  # 创建一个student对象
        session.add(fli)  # 添加到session
        session.commit()  # 提交到数据库



#Base.metadata.create_all()
#/index_3.html
for  i in range(1,309):
    if(i>1):
       scratch_latest_movies(scratch_url+"/index_"+str(i)+".html")